#!/usr/bin/env python
import getopt, sys, urllib, subprocess
import bitbucket, github, googlecode, sourceforge

SKIP_GIT = False
SKIP_SVN = False
SKIP_HG = False

try:
    import BeautifulSoup
except ImportError:
    SKIP_SVN = True

def usage():
    print 'USAGE: easy_django.py options install example-app\n'
    print 'OPTIONS:'
    print '-h, --help\t\tThis message'
    print '-q, --quiet\t\tDo not show anything on screen'
    print '-y, --force-yes\t\tForces yes on all questions'
    print '-d, --download-only\tOnly downloads the application, do not install it'

def check_for_vcs():
    vcs = {'GitHub' : 'git', 'Google Code' : 'svn', 'BitBucket' : 'hg', 'SourceForge' : 'svn'}
    for k,v in vcs.iteritems():
        try:
            subprocess.call(v, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        except OSError:
            globals()["SKIP_" + v.upper()] = True
            print 'Skipping ' + k + "... You don't seem to have " + v + " installed."

def search(arg):
    if not SKIP_GIT:
        gs = github.search(arg)
        print 'GitHub:\n'
        for each in gs:
            print each['name'] + ' - ' + each['description']
        else:
            if not gs:
                print 'Damn, nothing here.'
    if not SKIP_SVN:
        gc = googlecode.search(arg)
        print 'Google Code:\n'
        for each in gc:
            print each['name'] + ' - ' + each['description']
        else:
            if not gc:
                print 'Damn, nothing here.'

        sf = sourceforge.search(arg)
        print 'SourceForge:\n'
        for each in gc:
            print each['name'] + ' - ' + each['description']
        else:
            if not gc:
                print 'Damn, nothing here.'

def info(arg):
    if not SKIP_GIT:
        gi = github.info(arg)
        print 'Application Info:\n'
        for k,v in gi.iteritems():
            print k + ': ' + str(v)
    if not SKIP_SVN:
        gc = googlecode.info(arg)
        print 'Application Info:\n'
        for k,v in gc.iteritems():
            print k + ': ' + str(v)

        sf = sourceforge.info(arg)
        print 'Application Info:\n'
        for k,v in gc.iteritems():
            print k + ': ' + str(v)

def download(args, verbose=True):
    if not SKIP_GIT:
        for a in args:
            if verbose:
                print 'Downloading ' + a + ' from ' + github.NAME + '...'
            gi = github.download(a, verbose)
            if verbose:
                if gi:
                    print 'Download of ' + a + ' complete'
                else:
                    print "Haven't I told you that there was nothing here ?"
    if not SKIP_SVN:
        for a in args:
            if verbose:
                print 'Downloading ' + a + ' from ' + googlecode.NAME + '...'
            gc = googlecode.download(a, verbose)
            if verbose:
                if gc:
                    print 'Download of ' + a + ' complete'
                else:
                    print "Haven't I told you that there was nothing here ?"

        for a in args:
            if verbose:
                print 'Downloading ' + a + ' from ' + sourceforge.NAME + '...'
            sg = sourceforge.download(a, verbose)
            if verbose:
                if gc:
                    print 'Download of ' + a + ' complete'
                else:
                    print "Haven't I told you that there was nothing here ?"

def install(args):
    pass

def main():
    try:
        opts, args = getopt.gnu_getopt(sys.argv[1:], "hqyds:i:", ["help", "quiet", "force-yes", "download-only", "search=", "info="])
    except getopt.GetoptError, err:
        print str(err)
        usage()
        sys.exit(2)
    verbose = True
    force_yes = False
    download_only = False
    search_only = False
    info_only = False
    for o, a in opts:
        if o in ("-q", "--quiet"):
            verbose = False
        elif o in ("-h", "--help"):
            usage()
            sys.exit()
        elif o in ("-y", "--force-yes"):
            force_yes = True
        elif o in ("-d", "--download-only"):
            download_only = True
        elif o in ("-s", "--search"):
            search_only = True
            search_query = a
        elif o in ("-i", "--info"):
            info_only = True
            info_query = a
        else:
            assert False, "unhandled option"

    check_for_vcs()

    if search_only:
        if verbose: print 'Searching...'
        search(search_query)
    elif info_only:
        info(info_query)
    elif download_only:
        download(args, verbose)
    else:
        install(args)

if __name__ == "__main__":
    main()
